//
//  CommentListViewController.m
//  XDBaseFrameWork
//
//  Created by wanglong8889@126.com on 14-9-30.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "CommentListViewController.h"

#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
#import "UIImageView+MJWebCache.h"

@interface CommentListViewController ()
{
    int page;
    
    BOOL isfirst;
    
    UILabel * testLabel;
}

@end

@implementation CommentListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text = @"评价";
    
    page = 1;
    allCount = 1;
    
    isfirst = YES;
    dataArray = [[NSMutableArray alloc] init];
    
    [self initCommentsViews];
    
}

-(void)initCommentsViews{
    
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, UI_SCREEN_WIDTH, self.contentView.frame.size.height) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    
    dataArray = [[NSMutableArray alloc] initWithCapacity:4];
    
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    myTableView.showsVerticalScrollIndicator = NO;
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,UI_SCREEN_WIDTH,15)];
    view.backgroundColor = self.contentView.backgroundColor;
    myTableView.tableHeaderView = view;
    
    
    [myTableView addFooterWithTarget:self action:@selector(footerPulltoRefreshData)];
    [myTableView addHeaderWithTarget:self action:@selector(heaererPulltoRefreshData)];

    
    [self.contentView addSubview:myTableView];
    
    [self getListDatas:1];

}
#pragma mark  =======上下拉刷新========

-(void)heaererPulltoRefreshData
{
    page = 1;
    [self getListDatas:1];
}

-(void)footerPulltoRefreshData
{
    if (1) {
        page++;
        [self getListDatas:page];
    }else{
        if (1) {
            myTableView.footerRefreshingText = @"没有更多数据...";
            [self performSelector:@selector(delayToEndRefreshLoading) withObject:nil afterDelay:.5f];
        }
    }
}
-(void)delayToEndRefreshLoading
{
    [myTableView footerEndRefreshing];
    myTableView.footerRefreshingText = @"正在加载数据";
}



#pragma mark 获取列表数据
-(void)getListDatas:(int)mPage
{
    if (![XDTools NetworkReachable]){
        
        page = page==1?(1):(page-1);
        
        [XDTools showTips:brokenNetwork toView:self.view];
        [myTableView headerEndRefreshing];
        [myTableView footerEndRefreshing];
        return;
    }
    
    if (self.empleid == nil) {
        _empleid = @"";
    }
    
    //page 页码 pageSize 每页数据条数  EmplID  摄影师id
    NSDictionary * parms = @{@"page":[NSString stringWithFormat:@"%d",mPage],
                             @"pageSize":@"10",
                             @"EmplID":self.empleid,
                             @"SeriesID":self.seriesID};
    
    ASIHTTPRequest * request = [XDTools postRequestWithDict:parms API:API_GETCOMMENTS];
    __weak ASIHTTPRequest * mrequest = request;
    [request setCompletionBlock:^{
        
        [myTableView headerEndRefreshing];
        [myTableView footerEndRefreshing];
        
        [XDTools hideProgress:self.contentView];
        NSDictionary * tmpDict = [XDTools JSonFromString:[mrequest responseString]];
        if ([tmpDict[@"status"] intValue]==1){
            allCount = [[tmpDict valueForKey:@"Count"] intValue];
            if (page == 1){
                [dataArray removeAllObjects];
            }
            if (!(ARRAY_IS_NOT_EMPTY(tmpDict[@"data"][@"List"]))){
                [XDTools showTips:tmpDict[@"msg"] toView:self.view];
                page = page==1?(1):(page-1);
                [myTableView reloadData];
                return;
            }
            [dataArray addObjectsFromArray:tmpDict[@"data"][@"List"]];
            [myTableView reloadData];
            
        }else{
            
            page = page==1?(1):(page-1);
            
            [XDTools showTips:tmpDict[@"msg"] toView:self.view];
        }
    }];
    [request setFailedBlock:^{
        
        page = page==1?(1):(page-1);
        
        [myTableView headerEndRefreshing];
        [myTableView footerEndRefreshing];
        
        [XDTools hideProgress:self.contentView];
        [XDTools showTips:networkovertime toView:self.view];
    }];
    
    [request startAsynchronous];
    
    if (isfirst){
        [XDTools showProgress:self.contentView];
        isfirst = NO;
    }
}


#pragma  tableview 代理方
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifer = @"commentCell";
    
    CommentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    
    if (!cell) {
        cell = [[CommentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.delegate = self;
    cell.dataDict = [dataArray objectAtIndex:indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (!testLabel) {
        testLabel = [[UILabel alloc] init];
        testLabel.numberOfLines = 0;
        testLabel.font = [UIFont systemFontOfSize:15];
    }
    
    testLabel.frame = CGRectMake(0, 0, 300, 23);
    testLabel.text = [[dataArray objectAtIndex:indexPath.row] valueForKey:@"ReviewContent"];
    [testLabel sizeToFit];
    if ([[[dataArray objectAtIndex:indexPath.row] valueForKey:@"ReviewPhoto"] count]){
        return 10+50+10+testLabel.bounds.size.height+20+70+20;
    }
    
    return 10+50+10+testLabel.bounds.size.height+20;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark
#pragma mark CommentsTableViewCellDelegate
-(void)chooseImageView:(UIImageView *)iv andIvArray:(NSArray *)ivArray andImageUrlArray:(NSArray *)mImageUrlArray
{
    if (ARRAY_IS_NOT_EMPTY(mImageUrlArray)){
        int count = mImageUrlArray.count;
        NSMutableArray * photos = [NSMutableArray arrayWithCapacity:count];
        for (int i = 0; i<count; i++) {
            NSString *url = mImageUrlArray[i];
            MJPhoto * photo = [[MJPhoto alloc] init];
            photo.url = [NSURL URLWithString:url]; // 图片路径
            photo.srcImageView = [ivArray objectAtIndex:i];
            [photos addObject:photo];
        }
        
        // 2.显示相册
        MJPhotoBrowser * browser = [[MJPhotoBrowser alloc] init];
        browser.currentPhotoIndex = iv.tag-1000;
        browser.photos = photos; // 设置所有的图片
        [browser show];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
